package com.haoting.mamba.ucenter.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.haoting.common.utils.JwtUtils;
import com.haoting.common.utils.MD5;
import com.haoting.mamba.ucenter.entity.vo.LoginVo;
import com.haoting.mamba.ucenter.entity.vo.RegistVo;
import com.haoting.mamba.ucenter.util.EmailUtil;
import com.haoting.mamba.ucenter.util.VerificationCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoting.common.utils.PageUtils;
import com.haoting.common.utils.Query;

import com.haoting.mamba.ucenter.dao.UserInformationDao;
import com.haoting.mamba.ucenter.entity.UserInformationEntity;
import com.haoting.mamba.ucenter.service.UserInformationService;


@Service("userInformationService")
public class UserInformationServiceImpl extends ServiceImpl<UserInformationDao, UserInformationEntity> implements UserInformationService {

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    EmailUtil emailUtil;

    @Override
    public String login(LoginVo login){
        String username = login.getUsername();
        String password = login.getPassword();
        if(StringUtils.isEmpty(username)){
            return "0";
        }
        if(StringUtils.isEmpty(password)){
            return "00";
        }
        QueryWrapper<UserInformationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username).or()
                .eq("email",username);
        UserInformationEntity userInformationEntity  = baseMapper.selectOne(wrapper);
       int count =  baseMapper.selectCount(wrapper);
       if(count == 0){
            return "1";
       }

        if(!MD5.encrypt(password).equals(userInformationEntity.getPassword())){
            return "2";
        }
        String jwtToken = JwtUtils.getJwtToken(userInformationEntity.getId(),userInformationEntity.getNickname());
        return jwtToken;
    }

    @Override
    public String register(RegistVo registVo) {
        String code = registVo.getCode();
        String email = registVo.getEmail();
        String nickname = registVo.getNickname();
        String username = registVo.getUsername();
        String password = registVo.getPassword();
        String code2 = (String) redisTemplate.opsForValue().get(email);
        if(!code.equals(code2)){
            return "false";
        }
        if(StringUtils.isEmpty(email)
                ||StringUtils.isEmpty(nickname)||StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
            return "false";
        }
        QueryWrapper<UserInformationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("email",email)
                .or()
                .eq("username",username);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0){
            return "false";
        }
        UserInformationEntity userInformationEntity = new UserInformationEntity();
        userInformationEntity.setPassword(MD5.encrypt(password));
        userInformationEntity.setNickname(nickname);
        userInformationEntity.setUsername(username);
        userInformationEntity.setEmail(email);
        userInformationEntity.setAvatar("https://consumer-0329.oss-cn-chengdu.aliyuncs.com/avatar/avadefault.jpg");
        baseMapper.insert(userInformationEntity);
        return "true";
    }

    @Override


    public void sendCode(String email) {
        String code = new VerificationCode().achieveCode(4);
        redisTemplate.opsForValue().set(email,code,60, TimeUnit.SECONDS);
        emailUtil.sendTxtMail(email,code);

    }

    @Override
    public String checkCode(String email) {
        String code = (String) redisTemplate.opsForValue().get(email);
        return code;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserInformationEntity> page = this.page(
                new Query<UserInformationEntity>().getPage(params),
                new QueryWrapper<UserInformationEntity>()
        );

        return new PageUtils(page);
    }

}